package Messages;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mysql.jdbc.PreparedStatement;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * This class implements the Add Business to db
 * @author Gabi Revah
 * @author Aviad Almog
 */
public class MessageAddBusiness {
	
	private ConnectionToClient client ;
	private String[] str; 
	static int row1;

/**
 * Contractor of this class 
 * @param str
 * @param client
 */
	public MessageAddBusiness(String[] str, ConnectionToClient client){
		
		this.client= client ;
		this.str= str; 	
	}
	
/**
 * Adding Business customer to the DB	
 * @throws SQLException
 * @throws IOException
 */
	public void AddBusiness() throws SQLException, IOException{
	
				SQLConnection sc = new SQLConnection();
				java.sql.PreparedStatement bid = sc.getConnection().prepareStatement("SELECT * FROM Business;");
				java.sql.PreparedStatement busps = sc.getConnection().prepareStatement("INSERT INTO Business (BusinessId,Name,address,phone,contact,email) VALUES(?,?,?,?,?,?);");	
				
				ResultSet bidrs= bid.executeQuery();
				row1 = 0;
				while(bidrs.next()){
					int temp = bidrs.getRow();
						if(row1>temp)
							row1=row1;
							else{
							row1=temp;}
	
				}
										
										busps.setString(1,"B"+this.row1);
										busps.setString(2,str[1]);
										busps.setString(3,str[2]);
										busps.setString(4,str[3]);
										busps.setString(5,str[4]);
										busps.setString(6,str[5]);
										busps.executeUpdate();
										busps.close();
					
										ArrayList<Object> bus=new ArrayList<Object>();
										bus.add("AddBusiness");
										bus.add("B"+this.row1);

										
										client.sendToClient(bus);
											
									
								
	}
	

}

				
				        	
			


